import { login, logout, getUserInfo } from '@/api/salesman'
import { getToken, setToken, removeToken, getId, setId, setAgent, getName, setName } from '@/utils/auth'
import weui from 'weui.js'

const user = {
  state: {
    id: getId(),
    user: '',
    status: '',
    agent: '',
    token: getToken(),
    name: getName(),
    avatar: '',
    introduction: '',
    roles: [],
    setting: {
      articlePlatform: []
    }
  },

  mutations: {
    SET_ID: (state, id) => {
      state.id = id
    },
    SET_AGENT: (state, code) => {
      state.agent = code
    },
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_INTRODUCTION: (state, introduction) => {
      state.introduction = introduction
    },
    SET_SETTING: (state, setting) => {
      state.setting = setting
    },
    SET_STATUS: (state, status) => {
      state.status = status
    },
    SET_NAME: (state, name) => {
      state.name = name
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles
    }
  },

  actions: {
    // 用户名登录
    LoginByUsername({ commit }, userInfo) {
      const username = userInfo.username.trim()
      return new Promise((resolve, reject) => {
        login(userInfo).then(response => {
          const data = response.data
          if (data.success) {
            if (data.result.token) {
              commit('SET_TOKEN', data.result.token)
              commit('SET_ID', data.result.saleman_num)
              commit('SET_NAME', data.result.saleman_name)
              commit('SET_AGENT', data.result.agent_num)
              setToken(response.data.result.token)
              setName(data.result.saleman_name)
              setAgent(data.result.agent_num)
              setId(response.data.result.saleman_num)
              resolve(response.data.errorCode)
            } else {
              weui.alert(response.data.errorMsg)
              reject()
            }
          } else {
            weui.alert(data.errorMsg)
            reject()
          }
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 获取用户信息
    // GetUserInfo({ commit, state }) {
    //   return new Promise((resolve, reject) => {
    //     getUserInfo(state.id).then(response => {
    //       if (!response.data) { // 由于mockjs 不支持自定义状态码只能这样hack
    //         reject('error')
    //       }
    //       const data = response.data
    //       commit('SET_ROLES', [data.result.level]) // leval must be array
    //       commit('SET_AVATAR', data.result.avatar)
    //       // commit('SET_INTRODUCTION', data.introduction)
    //       resolve(response)
    //     }).catch(error => {
    //       reject(error)
    //     })
    //   })
    // },

    // // 登出
    // LogOut({ commit, state }) {
    //   return new Promise((resolve, reject) => {
    //     logout(state.id).then(() => {
    //       commit('SET_TOKEN', '')
    //       commit('SET_ROLES', [])
    //       removeToken()
    //       resolve()
    //     }).catch(error => {
    //       reject(error)
    //     })
    //   })
    // },

    // 前端 登出
    FedLogOut({ commit }) {
      return new Promise(resolve => {
        commit('SET_TOKEN', '')
        removeToken()
        resolve()
      })
    }
  }
}

export default user
